package com.bk.cyxf.log;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.object.IObject;
import jt.classic.system.object.ObjectLCManager;
import jt.classic.system.user.IUser;
import jt.classic.system.wordbook.WordBookUtil;
import net.sf.json.JSONObject;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.limp.basework.MachiningException;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.mine.DateTrimmer;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LogAction extends SimpleAction {
    public ActionForward list(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        String id = StringTool.checkString(param.get("id"));
        String start = StringTool.checkString(param.get("start"));
        String limit = StringTool.checkString(param.get("limit"));
        String pageNO = String.valueOf((Integer.parseInt(start) / Integer.parseInt(
                    limit)) + 1);
        String result = "";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            String xftype = null;

            String _id = null;

            if (object.ikey().equals("APP_LFDJ")) {
                Table tbl = object.iobject().itable("APP_LF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                _id = StringTool.checkString(info.get("LFID"));

                xftype = "1";
            } else if (object.ikey().equals("APP_LXDJ")) {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                _id = StringTool.checkString(info.get("LXID"));

                xftype = "0";
            }else if (object.ikey().equals("APP_QTFDJ")) {
                Table tbl = object.iobject().itable("APP_QTF");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                _id = StringTool.checkString(info.get("QTFID"));

                xftype = "2";
            }

            String sql1 = "SELECT count(*) FROM APP_XFLOG WHERE XFID = " + _id +
                " AND XF_TYPE = " + xftype + "";
            String sql2 =
                "SELECT LOGID,L_DATE,XFID,XF_TYPE,L_USERNAME,L_GROUPNAME,L_TYPE,L_MEMO FROM APP_XFLOG WHERE XFID = " +
                _id + " AND XF_TYPE = " + xftype + " ORDER BY L_DATE ";
            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, "");
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, limit);
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            List list = plus.work();
            Label label = plus.getLabel();
            ListRange lr = new ListRange();
            lr.setTotalSize(label.totaldata());
            
            if(list != null && !list.isEmpty()){
            	for(int i=0;i<list.size();i++){
            		Map info = (Map)list.get(i);
            		
            		String date = (String)info.get("L_DATE");
            		
            		info.put("L_DATE", DateTrimmer.getYMDHM_LBL(date));
            		
            		String type = (String)info.get("L_TYPE");
            		
            		info.put("L_TYPE", WordBookUtil.getWordBookItemName("xflog", type));
            	}
            }
            
            lr.setList(list);
            lr.setSuccess(true);
            result = JSONObject.fromObject(lr).toString();
        } catch (Exception ex) {
            result = "[]";
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        request.setAttribute("result", result);

        return mapping.findForward("result");
    }
    
    /**
     * 添加一条日志记录
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public void insert(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//    	String result = "";
    	Connection conn = null;
    	String id = StringTool.checkString(request.getParameter("id"));
    	String opera = StringTool.checkString(request.getParameter("opera")); 
    	String memo = StringTool.checkString(request.getParameter("memo")); 
    	String type  = StringTool.checkString(request.getParameter("type")); 
    	IUser user = ISystem.getSessionUser(request);
    	try {
    		SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();
            LogManager lm = new LogManager(conn);
            lm.log(user, id, type, opera, memo);
            conn.commit();
//            result = "{success: true}";
        } catch (Exception ex) {
        	ISystem.catchException(request, this, ex);
//        	result = "[]";
        }finally{
        	SystemDataSource.closeConnection(conn);
        }
//    	request.setAttribute("result", result);
//        return mapping.findForward("result");
    	
    }
}
